#include "model.h"
#include <QDebug>
Model::Model(int num)
{
    this->num=num;
    flag=new bool[num];
    distance=new int[num];
    for(int i=0;i<num;i++)
    {
        path.push_back(QVector<int>());
        flag[i]=false;
        distance[i]=INF;
    }
    finish=false;
}

Model::Model(const Model &another)
{
    this->num=another.num;
    flag=new bool[num];
    distance=new int[num];
    for(int i=0;i<num;i++)
    {
        path.push_back(another.path[i]);
        flag[i]=another.flag[i];
        distance[i]=another.distance[i];
    }
    solvePath=another.solvePath;
    current=another.current;
    finish=another.finish;
}

Model::~Model()
{
    delete[] flag;
    delete[] distance;
}

bool Model::step()
{
    //path[current].push_back(current);   //经过该点

    if(current==-1)     //如果已经无路可走
        return false;   //返回false
    //TODO 定义接口让子类实现，用于更新路径和距离
    updateDistance();
    /*找下一个最近的点走*/
    int next=-1;
    int minNum=INF;
    for(int i=0;i<num;i++)
    {
        if(!flag[i]&&minNum>distance[i])//如果没有经过这个点并且距离最小,并且不能是自身
        {
            minNum=distance[i];//更新最小距离
            next=i;//记录下一个要走的点
        }
    }

    if(next==-1)//如果无点可走
    {
        finish=true;
        return false;//返回false
    }
        //qDebug()<<"当前点为 "<<current<<"最小值为"<<minNum<<" 下一个点为"<<next<<"路径长度为"<<path[next].size();
    flag[next]=true;//标记该点已达
    current=next;
    solvePath.push_back(current);
    return true;
}
